package com.shanghaionstar.activity.webview.handler.picture;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.shanghaionstar.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 多图片选择 适配器
 */
public class LocalPicListAdapter extends BaseAdapter {

    private HashMap<Integer, Boolean> checkBoxMap = new HashMap<Integer, Boolean>();

    private List<ImportImageItem> resultList;

    private LayoutInflater inflater;

    private Context context;

    private float screenWidth;

    private int picWidth;

    private GridView gvFromLocal;
    private boolean isSupportMultiSelected = false;
    private ImportImageItem takeCameraItem;
    private int maxNumber;
    private Activity activity;

    public ArrayList<ImportImageItem> getSelectList() {
        ArrayList<ImportImageItem> selectList = new ArrayList<ImportImageItem>();
        if (resultList != null) {
            if (checkBoxMap != null && checkBoxMap.size() > 0) {
                Set<Integer> key = checkBoxMap.keySet();
                selectList.clear();
                for (Integer integer : key) {
                    selectList.add(resultList.get(integer));
                }
            }
        }
        return selectList;
    }


    /**
     * @param mContext
     * @param gridView
     * @param dataList
     * @param isSupportMultiSelected 是否选择多个文件
     */
    public LocalPicListAdapter(Context mContext, GridView gridView, List<ImportImageItem> dataList,
                               boolean isSupportMultiSelected, int maxNumber) {
        this.context = mContext;
        this.gvFromLocal = gridView;
        this.maxNumber = maxNumber;
        this.activity = activity;

        this.isSupportMultiSelected = isSupportMultiSelected;
        takeCameraItem = new ImportImageItem();
        takeCameraItem.isImageButton = true;
        dataList.add(0, takeCameraItem);
        this.resultList = dataList;
        inflater = LayoutInflater.from(context);
        DisplayMetrics dm = new DisplayMetrics();
        dm = mContext.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        picWidth = (int) (screenWidth / 3 - 20);

        // picHeight = (int) ((float) picWidth * 25 / 47);
    }

    @Override
    public int getCount() {
        if (resultList != null)
            return resultList.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return resultList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        ImportImageItem item = (ImportImageItem) getItem(position);
        if (item.isImageButton)
            return 1;
        return 2;
    }

    private class TakeCamera {
        ImageView takeCamera;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        TakeCamera takeCamera = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            if (type == 1) {
                convertView = inflater.inflate(R.layout.view_takecamera, null);
                takeCamera = new TakeCamera();
                takeCamera.takeCamera = (ImageView) convertView.findViewById(R.id.btn_camera);
                convertView.setTag(takeCamera);
                convertView.setBackgroundColor(context.getResources().getColor(R.color.black_color));

            } else {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_grid_localpic, parent, false);
                holder.imgLocalPic = (ImageView) convertView.findViewById(R.id.img_local_pic);
                holder.cbSelect = (CheckBox) convertView.findViewById(R.id.cb_select);
                convertView.setTag(holder);
                convertView.setBackgroundColor(context.getResources().getColor(R.color.trans));

            }

        } else {
            if (type == 1) {
                if (convertView.getTag() instanceof ViewHolder) {
                    convertView = inflater.inflate(R.layout.view_takecamera, null);
                    takeCamera = new TakeCamera();
                    takeCamera.takeCamera = (ImageView) convertView.findViewById(R.id.btn_camera);
                    convertView.setTag(takeCamera);
                } else {
                    takeCamera = (TakeCamera) convertView.getTag();

                }
                takeCamera.takeCamera.setLayoutParams(new RelativeLayout.LayoutParams(picWidth, picWidth));
                convertView.setBackgroundColor(context.getResources().getColor(R.color.black_color));

            } else {
                if (convertView.getTag() instanceof TakeCamera) {
                    holder = new ViewHolder();
                    convertView = inflater.inflate(R.layout.item_grid_localpic, parent, false);
                    holder.imgLocalPic = (ImageView) convertView.findViewById(R.id.img_local_pic);
                    holder.cbSelect = (CheckBox) convertView.findViewById(R.id.cb_select);
                    convertView.setTag(holder);

                } else {
                    holder = (ViewHolder) convertView.getTag();

                }
                convertView.setBackgroundColor(context.getResources().getColor(R.color.trans));

            }

        }

        if (type == 1) {
            takeCamera.takeCamera.setImageResource(R.drawable.camera);
            convertView.setBackgroundColor(context.getResources().getColor(R.color.black_color));

        } else {
            ImportImageItem imageItem = (ImportImageItem) getItem(position);

            if (isSupportMultiSelected) {
                // 多选
                holder.cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                        if (checkBoxMap.size() >= maxNumber && isChecked) {
                            buttonView.setChecked(false);
                            ToastUtils.showToast_Long(activity, "最多可以选择" + maxNumber + "张图片！");
                            return;
                        }

                        CheckBox checkedTextView = (CheckBox) buttonView;
                        if (checkedTextView.isChecked()) {
                            checkBoxMap.put(position, isChecked);
                        } else {

                            checkBoxMap.remove(position);
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                // 单选
                holder.cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        CheckBox checkedTextView = (CheckBox) buttonView;
                        if (checkedTextView.isChecked()) {
                            if (checkBoxMap != null && checkBoxMap.size() > 0)
                                checkBoxMap.clear();
                            checkBoxMap.put(position, isChecked);
                        } else {

                            checkBoxMap.remove(position);
                        }
                        notifyDataSetChanged();
                    }
                });
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewHolder holderTemp = (ViewHolder) v.getTag();
                    holderTemp.cbSelect.performClick();

                }
            });
            holder.imgLocalPic.setLayoutParams(new RelativeLayout.LayoutParams(picWidth, picWidth));
            if (checkBoxMap.containsKey(position) && checkBoxMap.get(position) != null) {
                holder.cbSelect.setChecked(true);
            } else {
                holder.cbSelect.setChecked(false);
            }

            Glide.with(context)
                    .load("file://" + imageItem.getImage())
                    .placeholder(R.drawable.default_image)
                    .crossFade()
                    .into(holder.imgLocalPic);
            convertView.setBackgroundColor(context.getResources().getColor(R.color.trans));

        }

        return convertView;
    }

    static class ViewHolder {
        ImageView imgLocalPic;

        CheckBox cbSelect;
    }

}
